package bma.common.thrift.sample;

import java.util.HashMap;
import java.util.Map;

import org.apache.thrift.TBase;
import org.apache.thrift.TException;

import bma.common.langutil.ai.stack.AIStack;
import bma.common.thrift.ai.AIBaseStack;
import bma.common.thrift.ai.AIThriftInvoker;
import bma.common.thrift.ai.TAIBaseProcessor;
import bma.common.thrift.ai.TAIBaseServiceClient;
import bma.common.thrift.ai.TAIProcessFunction;
import bma.common.thrift.sample.Hello.error_args;
import bma.common.thrift.sample.Hello.error_result;
import bma.common.thrift.sample.Hello.name_args;
import bma.common.thrift.sample.Hello.name_result;
import bma.common.thrift.sample.Hello.say_args;
import bma.common.thrift.sample.Hello.say_result;
import bma.common.thrift.sample.Hello.sleep_args;
import bma.common.thrift.sample.Hello.sleep_result;

public class Hello4AI {

	public static interface Iface {

		public boolean say(AIStack<Boolean> stack, String word)
				throws org.apache.thrift.TException;

		public boolean name(AIStack<String> stack, String title)
				throws org.apache.thrift.TException;

		public boolean error(AIStack<Boolean> stack, String msg)
				throws org.apache.thrift.TException;

		public boolean sleep(AIStack<Boolean> stack, int time)
				throws org.apache.thrift.TException;

	}

	public static class Processor<IFACE extends Iface> extends
			TAIBaseProcessor<IFACE> {

		public Processor(IFACE iface) {
			super(
					iface,
					getProcessMap(new HashMap<String, TAIProcessFunction<IFACE, ? extends TBase>>()));
		}

		private static <I extends Iface> Map<String, TAIProcessFunction<I, ? extends org.apache.thrift.TBase>> getProcessMap(
				Map<String, TAIProcessFunction<I, ? extends org.apache.thrift.TBase>> processMap) {
			processMap.put("say", new say());
			processMap.put("name", new name());
			processMap.put("error", new error());
			processMap.put("sleep", new sleep());
			return processMap;
		}

		private static class say<I extends Iface> extends
				TAIProcessFunction<I, say_args> {
			public say() {
				super("say");
			}

			@Override
			protected say_args getEmptyArgsInstance() {
				return new say_args();
			}

			protected boolean getResult(AIStack<TBase> stack, I iface,
					say_args args) throws org.apache.thrift.TException {
				say_result result = new say_result();
				return iface.say(new AIBaseStack<Boolean>(stack, result),
						args.getWord());
			};

		}

		private static class name<I extends Iface> extends
				TAIProcessFunction<I, name_args> {
			public name() {
				super("name");
			}

			@Override
			protected name_args getEmptyArgsInstance() {
				return new name_args();
			}

			@Override
			protected boolean getResult(AIStack<TBase> stack, I iface,
					name_args args) throws org.apache.thrift.TException {
				name_result result = new name_result();
				return iface.name(new AIBaseStack<String>(stack, result),
						args.getTitle());
			}
		}

		private static class error<I extends Iface> extends
				TAIProcessFunction<I, error_args> {
			public error() {
				super("error");
			}

			@Override
			protected error_args getEmptyArgsInstance() {
				return new error_args();
			}

			@Override
			protected boolean getResult(AIStack<TBase> stack, I iface,
					error_args args) throws org.apache.thrift.TException {
				error_result result = new error_result();
				return iface.error(new AIBaseStack<Boolean>(stack, result),
						args.getMsg());
			}
		}

		private static class sleep<I extends Iface> extends
				TAIProcessFunction<I, sleep_args> {
			public sleep() {
				super("sleep");
			}

			@Override
			protected sleep_args getEmptyArgsInstance() {
				return new sleep_args();
			}

			@Override
			protected boolean getResult(AIStack<TBase> stack, I iface,
					sleep_args args) throws org.apache.thrift.TException {
				error_result result = new error_result();
				return iface.sleep(new AIBaseStack<Boolean>(stack, result),
						args.getTime());
			}
		}
	}

	public static class Client extends TAIBaseServiceClient implements Iface {

		public Client(AIThriftInvoker invoker, String module) {
			super(invoker, module);
		}

		@Override
		public boolean say(AIStack<Boolean> stack, String word)
				throws TException {
			return super.invoke(stack, new say_result(), "say", new say_args(
					word));
		}

		@Override
		public boolean name(AIStack<String> stack, String title)
				throws TException {
			return super.invoke(stack, new name_result(), "name",
					new name_args(title));
		}

		@Override
		public boolean error(AIStack<Boolean> stack, String msg)
				throws TException {
			return super.invoke(stack, new error_result(), "error",
					new error_args(msg));
		}

		@Override
		public boolean sleep(AIStack<Boolean> stack, int time)
				throws TException {
			return super.invoke(stack, new sleep_result(), "sleep",
					new sleep_args(time));
		}
	}
}
